package com.handpay.nocardpay.baselibrary.download.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.view.activity.BaseActivity;
import com.handpay.nocardpay.baselibrary.download.widget.CheckUpdateTask;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.permission.PermissionRequest;
import com.handpay.nocardpay.baselibrary.permission.PermissionCode;
import com.handpay.nocardpay.baselibrary.utils.DialogUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

import java.util.List;

/**
 * Created by lyang on 2017/8/20.
 */

public class DownloadApkActivity extends BaseActivity implements PermissionRequest.onPermissionRequestCallBack {
    public final static String DOWNLOAD_APK_URL = "DOWNLOAD_APK_URL";
    public static final int MSG_DOWNING = 1; // 下载中
    public static final int MSG_FINISH = 2; // 下载完成
    public static final int MSG_FAILURE = 3;// 下载失败
    private WebView mWebview;
    private PermissionRequest mPermissionRequest;
    private String mDownloadUrl;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_DOWNING: {
                    int progress = msg.arg1;
                    HPLog.i(HPLog.YL_TAG, "shouldOverrideUrlLoading:MSG_DOWNING progress:" + progress);
                    DialogUtils.showHorizontalProgressDialog(DownloadApkActivity.this, "升级", "升级包下载进度", false, progress);
                    break;
                }
                case MSG_FINISH: {
                    DialogUtils.closeProgressDialog();
                    break;
                }
                case MSG_FAILURE: {
                    DialogUtils.closeProgressDialog();
                    break;
                }
                default:
                    break;
            }

        }
    };

    @Override
    public void initData() {
        String url = this.getIntent().getStringExtra(DOWNLOAD_APK_URL);
        mWebview.loadUrl(url);
        mPermissionRequest = new PermissionRequest(this, this);
//        mPermissionRequest.requestPermission(Permission.STORAGE);

    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        initToolbar(mToolbar, true, false, null);
        mWebview = (WebView) this.findViewById(R.id.download_webview);
        mWebview.getSettings().setDomStorageEnabled(true);
        mWebview.getSettings().setJavaScriptEnabled(true);
        mWebview.getSettings().setAppCacheEnabled(true);
        mWebview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        mWebview.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                HPLog.i(HPLog.YL_TAG, "shouldOverrideUrlLoading:" + url);
                if (url.contains(".apk")) {
                    mDownloadUrl = url;
                    mPermissionRequest.requestPermission(Permission.STORAGE);
                }
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_download_apk;
    }

    @Override
    public String getActivityTitle() {
        return getString(R.string.download_apk_title);
    }

    @Override
    public void permissionYes(List<String> permissions) {
        if (!TextUtils.isEmpty(mDownloadUrl)) {
            CheckUpdateTask task = new CheckUpdateTask(DownloadApkActivity.this, CheckUpdateTask.TYPE_NOTIFICATION, false, mHandler);
            task.startDownload(mDownloadUrl);
        }
    }

    @Override
    public void permissionFail(List<String> permissions) {
        if (AndPermission.hasAlwaysDeniedPermission(this, permissions)) {
            AndPermission.defaultSettingDialog(this, PermissionCode.REQUEST_CODE_SETTING).show();
        }
    }
}
